<template>
  <div class="monthWarn-box">
    <div class="monthWarn-content">
      <div class="title">本月报警趋势</div>
      <!--本月报警echarts趋势图-->
      <div class="month-warn"></div>
      <!--报警总数-->
      <div class="monthWarndata">{{eventCount}}</div>
    </div>
  </div>

</template>

<script>
export default {
  name: 'monthWarnTrend',
  data () {
    return {
      eventCount: 0
    }
  },
  mounted () {
    let url = this.$base.getUrl('summaryCurrentMonthEvent')
    let params = {
      url: url,
      data: {}
    }
    this.$store.dispatch('postForm', params).then(res => {
      if (res.code === 0) {
        // console.log(res.result)
        this.echartFun(res.result)
      }
    })
  },
  methods: {
    echartFun (result) {
      this.eventCount = result.eventCount
      let datas = []
      let echartData = []
      for (let i = 0; i < result.maxDay; i++) {
        let day = i + 1
        echartData.push({
          value: day,
          textStyle: {
            fontSize: 10,
            color: '#9FACC3'
          }
        })

        let flag = false
        for (let j = 0; j < result.detailList.length; j++) {
          if (day === result.detailList[j].day) {
            flag = true
            datas.push(result.detailList[j].eventCount)
            break
          }
        }

        if (!flag) {
          datas.push(0)
        }
      }

      let charts = this.$echarts.init(
        document.getElementsByClassName('month-warn')[0]
      )
      let option = {
        color: ['#3398DB'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          left: '0',
          right: '0',
          top: '40%',
          bottom: 0,
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: echartData,
            axisTick: {
              alignWithLabel: true,
              show: false
            },
            splitLine: {
              show: false,
              lineStyle: {
                color: '#9FACC3',
                opacity: '0.5'
              }
            },
            axisLine: {
              lineStyle: {
                color: '#9FACC3',
                opacity: '0.3'
              }
            }
          }
        ],
        yAxis: [
          {
            axisTick: { show: false },
            type: 'value',
            boundaryGap: true,
            // min:2,
            splitNumber: 3,
            axisLabel: {
              show: true,
              textStyle: {
                color: '#9FACC3'
              }
            },
            axisLine: {
              lineStyle: {
                width: 0
              }
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: '#9FACC3',
                opacity: '0.3'
              }
            }
          }
        ],
        series: [
          {
            name: '当日告警数',
            type: 'bar',
            barGap: '8%',
            barWidth: 3,
            // barWidth: "60%",
            itemStyle: {
              normal: {
                color: new this.$echarts.graphic.LinearGradient(
                  0, 0, 0, 1,
                  [
                    { offset: 0, color: '#F57BB7' },
                    { offset: 1, color: '#EFB486' }
                  ]
                ),
                label: {
                  show: false,
                  position: 'top'
                }
              }
            },
            data: datas
          }
        ]
      }
      charts.setOption(option, true)
    }
  },
  computed: {},
  watch: {}
}
</script>

<style scoped>
.monthWarn-box {
  width: 100%;
  margin-bottom: 10px;
}

.monthWarn-content {
  position: relative;
  width: 100%;
  background: rgba(43, 54, 72, 0.7);
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
  padding: 13px;
  box-sizing: border-box;
}
.monthWarn-content .title {
  position: relative;
  top: 8px;
  left: 10px;
  color: #ffffff;
  font-size: 12px;
  font-weight: 600;
}

.month-warn {
  width: 100%;
  height: 130px;
}

.monthWarndata {
  position: absolute;
  top: 4%;
  right: 9%;
  font-size: 30px;
  color: #f5a623;
}
</style>
